<template>
  <div>
    <uploadExcel :onSuccess="onSuccess"></uploadExcel>
  </div>
</template>

<script>
import { importEmployeeApi } from "@/api/employee";
export default {
  name: "",
  mounted() {
    //console.log(this.$children[0].handleUpload(), 1112211);
  },
  methods: {
    async onSuccess({ header, results }) {
      // console.log(header, 7);
      // console.log(results, 8);
      /*
        result=[
          {
            入职日期: 43535
            姓名: "高大山"
            工号: 20089
            手机号: 13041130879
            转正日期: 43719
          }
        ]

        */
      const userRelations = {
        入职日期: "timeOfEntry",
        手机号: "mobile",
        姓名: "username",
        转正日期: "correctionTime",
        工号: "workNumber"
      };

      //var qq = Object.keys(userRelations);
      //console.log(qq, 1234); //  ["入职日期", "手机号", "姓名", "转正日期", "工号"] 123
      // qq.forEach(item => {  //  这里获取的是数组的每一项
      //   //console.log(results[item],88899);

      /* 
      [
        {timeOfEntry:43535,}
      ]
      
       */

      // });
      var arr = [];
      results.forEach(item => {
        //console.log(item[], 8888);
        var obj = {};
        Object.keys(userRelations).forEach(key => {
          //  ["入职日期", "手机号", "姓名", "转正日期", "工号"]
          //  item[key]  获取的是  高大山
          /* 
          userRelations[key]====timeOfEntry  mobile
          
           */
          //userRelations[key]=item[key] ==值

          if (
            userRelations[key] == "timeOfEntry" ||
            userRelations[key] == "correctionTime"
          ) {
            obj[userRelations[key]] = new Date(this.formatDate(item[key], "/"));
            return;
          }
          obj[userRelations[key]] = item[key];
        });
        //console.log(obj, 11111);
        arr.push(obj);
      });

      //console.log(arr,445566);
      await importEmployeeApi(arr);
      this.$router.push("/employees");
    },

    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1);
      time.setYear(time.getFullYear() - 70);
      const year = time.getFullYear() + "";
      const month = time.getMonth() + 1 + "";
      const date = time.getDate() - 1 + "";
      if (format && format.length === 1) {
        return year + format + month + format + date;
      }
      return (
        year +
        (month < 10 ? "0" + month : month) +
        (date < 10 ? "0" + date : date)
      );
    }

    /* 
    res=[
      {username:jack,age:18,code:1001,salary:3000},
      {username:jack2,age:19,code:1001,salary:4000},
      {username:jack3,age:20,code:1001,salary:5000},
      ....
      38个
    ]

    res2=[
      [jack,18,3000],
      [jack2,19,4000],
      [jack3,20,5000],
      [],
      ...
      38个
    ]
    
    
     */
  }
};
</script>
<style lang="less" scoped></style>
